"use strict";

function Prop()
{
    this.m_id="";
    this.m_text=null;
    this.m_x=-1;
    this.m_y=-1;
    this.m_color=SGFProp.COLOR_NONE;
    this.m_visited=false;
    this.dump=function()
    {
        console.log(4,this.m_id+"["+this.m_x+" "+this.m_y+"]");
    }
    this.m_index=0;
}

function Node()
{
    this.m_str="";
    this.m_next=null;
    this.m_prev=null;
    this.m_childs=null;
    this.m_parent=null; 
    this.m_stone=null;
    this.m_comment=null;    
    this.m_prop=[];
    this.decoded=false;
    this.m_capture=[];
    this.getPropStr=function(p)
    {
        var prop;
        prop=this.getProp(p);
        if(prop==null)
            return "";
        else
            return prop;
    }
}

Node.prototype.addChild=function(node)
{
    if(this.m_childs==null)
        this.m_childs=new Array();
    node.m_parent=this;
    this.m_childs.push(node);
}

Node.prototype.dump=function()
{
    var i;   
    for(i=0;i<this.m_prop.length;i++)
    {
        p=this.m_prop[i];
        str=p.m_id+"-"+p.m_x+":"+p.m_y+"-"+p.m_text;
        console.log(str);
    }
}

Node.prototype.decodeSGFNode=function()
{
    var arr,p_info,props,i,j,p_id;
    if(this.m_str!="")
    {
        arr=this.m_str.match(/[A-Z]+(\[(.|\s)*?\])+/g);        
        if(arr!=null)
        {
            for(i=0;i<arr.length;i++)
            {
                props=arr[i].split("[");
                //cut last ']'            
                p_id=props[0];
                for(j=1;j<props.length;j++) 
                {                      
                    p_info=props[j].substring(0,props[j].length-1);
                    this.addProp(p_id,p_info);
                }
            }
        }        
     }
}

Node.prototype.dumpPov=function(p_id,x,y)
{
    var str;
    /*
    if((p_id != SGFProp.ID_B) || (p_id != SGFProp.ID_W))
    {
        console.log(p_id);
        return;
    }
    */
    str="object {";
    if(p_id==SGFProp.ID_B)
    {
        str+="b_stone(st)";
    }
    else if(p_id==SGFProp.ID_W)
    {
        str+="w_stone(st)";
    }
    str+="translate goban_at("+x+","+y+") }";
    console.log(str);
    //object {w_stone(st) translate goban_at(2,2) }
}

Node.prototype.addProp=function(p_id,p_info)
{
    var p=null;    
    switch(p_id)
    {
        case SGFProp.ID_SZ:
		case SGFProp.ID_PW:
		case SGFProp.ID_PB:
		case SGFProp.ID_WR:
		case SGFProp.ID_BR:
		case SGFProp.ID_EV:
		case SGFProp.ID_DT:
		case SGFProp.ID_KM:
		case SGFProp.ID_RE:
		case SGFProp.ID_PL:
		case SGFProp.ID_C:
                         p=new Prop();
                         p.m_id=p_id;
                         p.m_text=p_info;
                         if(p.m_id==SGFProp.ID_C)
                            this.m_comment=p_info;
                         else
                            this.m_prop.push(p);
                         break;
        case SGFProp.ID_B:
        case SGFProp.ID_W:
        case SGFProp.ID_AW:
        case SGFProp.ID_AB:
        case SGFProp.ID_LB:
        case SGFProp.ID_TR:
        case SGFProp.ID_SQ:                        
                        p=new Prop();
                        p.m_id=p_id;
                        p.m_x=p_info.charCodeAt(0)-97;
                        p.m_y=p_info.charCodeAt(1)-97;
                        //this.dumpPov(p.m_id,p.m_x,p.m_y);
                        if(p_id==SGFProp.ID_LB)
                            p.m_text=p_info.toUpperCase().substr(3);
                            
                        if((p_id==SGFProp.ID_B) || (p_id==SGFProp.ID_AB))
                            p.m_color=SGFProp.COLOR_BLACK;                            
                        else if((p_id==SGFProp.ID_W) || (p_id==SGFProp.ID_AW))
                            p.m_color=SGFProp.COLOR_WHITE;
                            
                        if((p_id==SGFProp.ID_B) || (p_id==SGFProp.ID_W))                            
                            this.m_stone=p;
                        else
                            this.m_prop.push(p);                                             
                            
                        break;                                                 
    }     
}

Node.prototype.getProp=function(prop_id)
{
    var i;
    for(i=0;i<this.m_prop.length;i++)
    {
        if(this.m_prop[i].m_id==prop_id)
        {
            return this.m_prop[i].m_text;
        }
    }
    return null;
}

Node.prototype.getNumberOfChilds=function()
{

    if(this.m_childs!=null)
    {
		return this.m_childs.length;
    }
	return 0;

}
Node.prototype.getChild=function(ind)
{
	if(this.m_childs!=null)
	{
		if(ind<this.m_childs.length)
		{
			return this.m_childs[ind];
		}
	}
    return null;
}

Node.prototype.getNext=function()
{
	return this.m_next;
}

function SGoGameTree()
{
    this.m_root=null;
    this.m_current=null;
    this.lstack=new Array();
    this.MAX_NODES=1000;
    this.m_bounds={left:20,top:20,right:0,bottom:0}     
}

SGoGameTree.prototype.addNode=function(node)
{
    node.gametree=this;
    if(this.m_root==null)
    {
        this.m_root=node;
        this.m_current=this.m_root;
    }
    else
    {
        if(this.m_current==null)
        {
            console.error("SGoGameTree: Invalid structure");
            return;
        }
        if(this.m_current.m_next==null)
        {
            this.m_current.m_next=node;
            node.m_prev=this.m_current;
            this.m_current=node;
        }
        else
        {
           this.m_current.addChild(node);
           this.m_current=node;
        }
    }
}

SGoGameTree.prototype.getCurrent=function()
{
    return this.m_current;
}


SGoGameTree.prototype.goRoot=function()
{
  this.m_current=this.m_root;
}

SGoGameTree.prototype.moveNext=function()
{
    if((this.m_current!=null) && (this.m_current.m_next!=null))
    {
        this.m_current=this.m_current.m_next;
        return true;
    }
    return false;
}

SGoGameTree.prototype.movePrev=function()
{
    if(this.m_current!=null)
    {
        if(this.m_current.m_prev!=null)
        {
            this.m_current=this.m_current.m_prev;
            return true;
        }
        else
        {
            if(this.m_current.m_parent!=null)
            {
                this.m_current=this.m_current.m_parent;
                return true;
            }
            
        }
    }
    return false;
}

SGoGameTree.prototype.moveParent=function()
{
    if(this.m_current!=null && this.m_current.m_parent!=null)
    {
        this.m_current=this.m_current.m_parent;
        return true;
    }
    return false;
}

SGoGameTree.prototype.moveChild=function(childnr)
{
    if((this.m_current!=null) && (this.m_current.m_childs!=null))
    {
        if(childnr<this.m_current.m_childs.length)
        {
            this.m_current=this.m_current.m_childs[childnr];
            return true;
        }
    }
    return false;
}

SGoGameTree.prototype.findNextMove=function(x,y)
{
    var node;
    node=this.m_current.m_next;
    if((node !=null)&& (node.m_stone!=null))
    {
        if((node.m_stone.m_x==x)&&(node.m_stone.m_y==y))
            return true;
    }
    return false;
}

SGoGameTree.prototype.findChildMove=function(x,y)
{
    var i,node,cnode;
    cnode=this.m_current;
    if((cnode!=null) &&(cnode.m_childs!=null))
    {
        for(i=0;i<cnode.m_childs.length;i++)
        {
            node=cnode.m_childs[i];
            if((node !=null)&& (node.m_stone!=null))
            {
                if((node.m_stone.m_x==x)&&(node.m_stone.m_y==y))
					return i;
            }
        }
    }
    return -1;
}
SGoGameTree.prototype.getNextCurrent=function()
{
    var cnode;
	cnode=this.m_current;
	if(this.m_current!=null)
	{
		return this.m_current.m_next;
	}
	return null;
}

SGoGameTree.prototype.pushCurrent=function()
{
    this.lstack.push(this.m_current);
}

SGoGameTree.prototype.popCurrent=function()
{
    this.m_current=this.lstack.pop();
}

SGoGameTree.prototype.computeBounds=function(node)
{
    var i,p;
    /*stone is missing here */
    
    for (i=-1;i<node.m_prop.length;i++)
    {
        if(i==-1)             
        {
            p=node.m_stone;
            if(p==null)
                continue;
        }    
        else
            p=node.m_prop[i];            
                    
        if((p.m_x>=0) && (p.m_y>=0))
        {           
           if(this.m_bounds.left > p.m_x) 
                this.m_bounds.left=p.m_x;
                
           if(this.m_bounds.right<p.m_x)
                this.m_bounds.right=p.m_x;
                
           if(this.m_bounds.top > p.m_y) 
                this.m_bounds.top=p.m_y;
                
           if(this.m_bounds.bottom < p.m_y)
                this.m_bounds.bottom = p.m_y;           
           
        }
    }
}
SGoGameTree.prototype.traverse=function(root,control)
{
    var i;
    if(control<this.MAX_NODES && root!=null)
    {
        
        root.decodeSGFNode();        
        this.computeBounds(root);        

        this.traverse(root.m_next,control++);
        
        if(root.m_childs!=null)
        {
            for(i=0;i<root.m_childs.length;i++)
            {
               
               this.traverse(root.m_childs[i],control++);          
            }
        }
     }
}

SGoGameTree.prototype.encodeSGF=function(root,control,rawsgf)
{
    var i;
    if(control<this.MAX_NODES && root!=null)
    {
     
        root.encodeSGFNode();        
        this.computeBounds(root);        

        if(root.m_childs!=null)
           rawsgf+"(";
           
        this.traverse(root.m_next,control++);
        
        if(root.m_childs!=null)
        {
            rawsgf+=")";
            for(i=0;i<root.m_childs.length;i++)
            {
               rawsgf+="(";
               
               this.traverse(root.m_childs[i],control++);
                rawsgf+=")";
            }
        }
     }
     return rawsgf;
}
